package com.lagou.server;

import com.lagou.model.Questions;
import com.lagou.model.Student;
import com.lagou.model.StudentMessage;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class ExamMD {
    //管理员添加
    public void add(Questions q, ArrayList<Questions> arrayList) {
        arrayList.add(q);
    }
    //管理员删除
    public void delete(Questions q,ArrayList<Questions> arrayList) {
        for (int i=0;i<arrayList.size();i++){
            if (arrayList.get(i).getTitleNum()==q.getTitleNum()){
                arrayList.remove(i);
                break;
            }
        }
        System.out.println("删除成功");
    }
    //管理员查询
    public Questions findOF(Questions q,ArrayList<Questions> arrayList) {
        Questions questions = null;
        boolean flag = false;
        for (int i=0;i<arrayList.size();i++){
            if (arrayList.get(i).getTitleNum()==q.getTitleNum()){
                System.out.println("查询成功！");
                System.out.println("该题目为"+arrayList.get(i));
               questions=arrayList.get(i);
                flag=true;
                break;
            }
            flag=false;
        }if (!flag){
            System.out.println("该题目不存在！");
        }
        return questions;
    }
    //管理员修改
    public void update(Questions q,ArrayList<Questions> arrayList) {
        Boolean flag=false;
        for (int i=0;i<arrayList.size();i++){
            if (arrayList.get(i).getTitleNum()==q.getTitleNum()){
                arrayList.get(i).setTitle(q.getTitle());
                arrayList.get(i).setOption(q.getOption());
                arrayList.get(i).setResult(q.getResult());
                flag=true;
                break;
            }
        }
        if (flag){
            System.out.println("修改成功!");
        }else {
            System.out.println("该题目不存在！");
        }
    }
    //学生考试

    //从硬盘读入信息
    public void readExam(ArrayList<Questions> arrayList) throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("G:/file2.txt"));
        ArrayList<Questions> a=(ArrayList<Questions>) objectInputStream.readObject();

        for (Questions s:a
        ) {
            arrayList.add(s);
        }
        System.out.println("考题加载成功");
    }
    //退出后学生信息系统，保存在硬盘里
    public void saveExam(ArrayList<Questions> arrayList) throws IOException {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("G:/file2.txt"));
        objectOutputStream.writeObject(arrayList);
        System.out.println("考题成功保存到硬盘的文件中");


    }
}
